package com.example.okhttp0306;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private Button btn_get1;
    private Button btn_post1;
    private Button btn_get2;
    private Button btn_post2;
    private OkHttpClient okHttpClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        okHttpClient = new OkHttpClient();//1.先new出OkHttpClient
        btn_get1Click();
        btn_get2Click();
        btn_post2Click();
        btn_post1Click();
        //requestCityList();
    }

    /**
     * 控件初始化
     */
    private void initView() {
        btn_get1 = findViewById(R.id.btn_get1);
        btn_get2=findViewById(R.id.btn_get2);
        btn_post1=findViewById(R.id.btn_post1);
        btn_post2=findViewById(R.id.btn_post2);
    }

    /**
     * 点击按钮进行get同步请求
     */
    private void btn_get1Click(){
        btn_get1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        //默认为get请求
                        Request request = new Request.Builder().url("https://www.httpbin.org/get?a=1&b=2").build();
                        //准备好请求的call对象
                        Call call = okHttpClient.newCall(request);
                        try {
                            Response execute = call.execute();
                            Log.d(TAG,"execute"+execute.body().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        super.run();
                    }
                }.start();
            }
        });
    }

    /**
     * 点击按钮进行get异步请求
     */
    private void btn_get2Click(){
        btn_get2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //默认为get请求
                Request request = new Request.Builder().url("https://www.httpbin.org/get?a=1&b=2").build();
                //准备好请求的call对象
                Call call = okHttpClient.newCall(request);
                //异步请求
                //方法内部就会创建子线程，所以不需要我们手动创建子线程
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        //意味着我们跟服务器的通信是成功的，不代表响应数据会成功
                        if (response.isSuccessful()){//响应码在200-300之间就是成功的，所以在这儿进行判断
                            Log.d(TAG,"execute"+response.body().string());
                        }
                    }
                });
            }
        });
    }

    /**
     * 点击按钮进行post同步请求
     */
    private void btn_post1Click(){
        btn_post1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        //post请求必须传入一个RequestBody，也就是请求体
                        //get请求，参数直接加在url后面即可例如：?a=1&b=2
                        FormBody build = new FormBody.Builder().add("a", "1").add("b", "2").build();
                        Request request = new Request.Builder().url("https://www.httpbin.org/post")
                                .post(build).build();
                        //准备好请求的call对象
                        Call call = okHttpClient.newCall(request);
                        try {
                            Response response = call.execute();
                            Log.d(TAG,"post同步"+response.body().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        super.run();
                    }
                }.start();
            }
        });

    }

    /**
     * 点击按钮进行post异步请求
     */
    private void btn_post2Click(){
        btn_post2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //post请求必须传入一个RequestBody，也就是请求体
                //get请求，参数直接加在url后面即可例如：?a=1&b=2
                FormBody build = new FormBody.Builder().add("a", "1").add("b", "2").build();
                Request request = new Request.Builder().url("https://www.httpbin.org/post")
                        .post(build).build();
                //准备好请求的call对象
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        Log.d(TAG,"post异步"+response.body().string());
                    }
                });
            }
        });

    }

    private void requestCityList(){
        new Thread(){
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url("http://apis.juhe.cn/simpleWeather/cityList?key=8149094547f3e900f1ea47e6a25de938")
                        .build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    Log.d(TAG,"数据"+response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }

                super.run();
            }
        }.start();

    }



}